package com.example.test_map.tv;

import android.content.Context;
import android.view.View;

import com.tencent.tencentmap.mapsdk.maps.CameraUpdate;
import com.tencent.tencentmap.mapsdk.maps.CameraUpdateFactory;
import com.tencent.tencentmap.mapsdk.maps.MapView;
import com.tencent.tencentmap.mapsdk.maps.TencentMap;
import com.tencent.tencentmap.mapsdk.maps.model.LatLng;
import com.tencent.tencentmap.mapsdk.maps.model.Marker;
import com.tencent.tencentmap.mapsdk.maps.model.MarkerOptions;

import java.util.Map;

import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.platform.PlatformView;

public class MyTextView implements PlatformView, MethodChannel.MethodCallHandler, MapLife{
    private final MapView mapView;
    Context context;
    public MyTextView(Context context, BinaryMessenger messenger, int id, Map<String, Object> params) {
        this.context = context;


        mapView = new MapView(context);

        CameraUpdate cameraSigma = CameraUpdateFactory.newLatLng(new LatLng(23.071218,113.238425));
        mapView.getMap().moveCamera(cameraSigma); //移动地图







//开启信息窗口




        LatLng position = new LatLng(23.071218,113.238425);
        MarkerOptions options = new MarkerOptions(position);
        options.infoWindowEnable(false);//默认为true
        options.title("天安门")//标注的InfoWindow的标题
                .snippet("地址: 北京市东城区东长安街");//标注的InfoWindow的内容;//设置自定义Marker图标
        Marker marker = mapView.getMap().addMarker(options);

        marker.setInfoWindowEnable(true);
        mapView.getMap().setOnMarkerClickListener(new TencentMap.OnMarkerClickListener(){
            @Override
            public boolean onMarkerClick(Marker marker) {
                System.out.println("点击标签");
                return true;
            }
        });
        //设置Marker支持点击
        marker.setClickable(true);
//显示信息窗口
        marker.showInfoWindow();

        mapView.getMap().setTrafficEnabled(true);
        mapView.onStop();


//        TextView myNativeView = new TextView(context);
//        myNativeView.setText("我是来自Android的原生TextView");
//        if (params.containsKey("myContent")) {
//            String myContent = (String) params.get("myContent");
//            myNativeView.setText(myContent);
//        }
//        this.mapView = myNativeView;




        MethodChannel methodChannel = new MethodChannel(messenger, "plugins.nightfarmer.top/myview_" + id);
        methodChannel.setMethodCallHandler(this);
        System.out.println("构建新的样式TextView");
    }

    @Override
    public View getView() {
        return mapView;
    }

    @Override
    public void dispose() {

    }

    @Override
    public void onMethodCall(MethodCall call, MethodChannel.Result result) {
        if ("setText".equals(call.method)) {
            result.success(null);
        }else if ("onCreate".equals(call.method)){
            mapView.onStart();
            System.out.println("原生TextView反应事件：onCreate");
            result.success(null);

        }else if ("onResume".equals(call.method)){
            mapView.onResume();
            System.out.println("原生TextView反应事件：onResume");
            result.success(null);
        }else if ("onPause".equals(call.method)){
            mapView.onPause();
            System.out.println("原生TextView反应事件：onPause");
            result.success(null);
        }else if ("onDestroy".equals(call.method)){
            mapView.onDestroy();
            System.out.println("原生TextView反应事件：onDestroy");
            result.success(null);
        }
    }

    @Override
    public void onStart() {
        mapView.onStart();
    }

    @Override
    public void onResume() {
        mapView.onResume();
    }

    @Override
    public void onPause() {
        mapView.onPause();
    }

    @Override
    public void onStop() {
        mapView.onStop();
    }

    @Override
    public void onDestroy() {
        mapView.onDestroy();
    }
}
